<style lang="less">
  @import '../../styles/common.less';
</style>

<template>
<div style="width: 480px; margin-top: 20px;">
  <Form ref="stduentData" :model="stduentData" :rules="ruleValidate" :label-width="110">
    <FormItem label="学生姓名" prop="stduentName">
      <Input v-model="stduentData.stduentName" placeholder="请输入姓名"></Input>
    </FormItem>
    <FormItem label="学生学号" prop="stduentNo">
      <Input v-model="stduentData.stduentNo" placeholder="请输入学号"></Input>
    </FormItem>
    <FormItem label="学生性别" prop="stduentSex">
      <Input v-model="stduentData.stduentSex" placeholder="请输入性别"></Input>
    </FormItem>
    <FormItem label="学生手机号" prop="stduentPhone">
      <Input v-model="stduentData.stduentPhone" placeholder="请输入手机号"></Input>
    </FormItem>
    <FormItem label="学生身份证" prop="stduentIdCard">
      <Input v-model="stduentData.stduentIdCard" placeholder="请输入身份证"></Input>
    </FormItem>
    <FormItem label="学生所在班级" prop="stduentClass">
      <Input v-model="stduentData.stduentClass" placeholder="请输入班级"></Input>
    </FormItem>
    <FormItem label="学生所在院系" prop="stduentCollege">
      <Input v-model="stduentData.stduentCollege" placeholder="请输入院系"></Input>
    </FormItem>


    <FormItem>
      <Button type="primary" @click="handleSubmit('stduentData')">提交</Button>
      <Button type="ghost" @click="handleReset('stduentData')">清空</Button>
    </FormItem>
  </Form>
        </div>
</template>

<script>
    export default {
        name: "addstduent",
        data(){
          return{
            stduentData:{
              stduentName:'',
              stduentNo:'',
              stduentSex:'',
              stduentPhone:'',
              stduentIdCard:'',
              stduentClass:'',
              stduentCollege:'',
              id:0
            },
            ruleValidate:{
              stduentName:[
                {required: true,message:'请输入姓名',trigger:'blur'}
              ],
              stduentNo:[
                {required: true,message:'请输入学号',trigger:'blur'}
              ],
              stduentSex:[
                {required: true,message:'请输入性别',trigger:'blur'}
              ],
              stduentPhone:[
                {required: true,message:'请输入手机号',trigger:'blur'}
              ],
              stduentIdCard:[
                {required: true,message:'请输入身份证',trigger:'blur'}
              ],
              stduentClass:[
                {required: true,message:'请输入班级',trigger:'blur'}
              ],
              stduentCollege:[
                {required: true,message:'请输入院系',trigger:'blur'}
              ]
            }
          }
        },
        methods:{
          handleSubmit(name){
            this.$refs[name].validate((valid) => {
              if (valid) {
                let dataArr = {
                  stduentName:this.stduentData.stduentName,
                  stduentNo:this.stduentData.stduentNo,
                  stduentSex:this.stduentData.stduentSex,
                  stduentPhone:this.stduentData.stduentPhone,
                  stduentIdCard:this.stduentData.stduentIdCard,
                  stduentClass:this.stduentData.stduentClass,
                  stduentCollege:this.stduentData.stduentCollege,
                  id:this.stduentData.id
                };

                if(this.stduentData.id > 0){
                  this.$http.post('/school/updStduent', dataArr).then(res => {
                    this.$Message.success('更新成功!');
                    this.handleReset(name);
                  });
                }else {
                  this.$http.post('/school/addStd', dataArr).then(res => {
                    this.$Message.success('添加成功!');
                    this.handleReset(name);
                  });
                }
              }
            })
          },
          handleReset (name) {
            this.$refs[name].resetFields();
          }
        },
      mounted () {
        if(this.$route.query.id == undefined) return;
        this.$http.get('/school/getStduentById?id='+this.$route.query.id).then(res=>{
          var dt = res.data.data;
          this.stduentData.id = dt.id;
          this.stduentData.stduentName = dt.stduentName;
          this.stduentData.stduentNo = dt.stduentNo;
          this.stduentData.stduentSex = dt.stduentSex;
          this.stduentData.stduentPhone = dt.stduentPhone;
          this.stduentData.stduentIdCard = dt.stduentIdCard;
          this.stduentData.stduentCollege = dt.stduentCollege;
          this.stduentData.stduentClass = dt.stduentClass;

        });
      },
      created () {

      }
    }
</script>

<style scoped>

</style>
